//
//  DeviceListCell.m
//  OST
//
//  Created by mac on 13-7-6.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "DeviceListCell.h"
#import "CommonData.h"
#import "Utitlity.h"

@implementation DeviceListCell
@synthesize labelName,icon,btn_refresh;
@synthesize btn_arm,btn_ds_arm,ipUrl;

-(void)initView
{
    UIImage *img1  = [UIImage imageNamed:@"bg_cell"];
    UIImage *img2  = [UIImage imageNamed:@"bg_cell_sel"];
    
    UIImageView *cellbg1 = [[UIImageView alloc]initWithImage:img1];
    UIImageView *cellbg2 = [[UIImageView alloc]initWithImage:img2];
    
    self.backgroundView = cellbg1;
    self.selectedBackgroundView = cellbg2;
}


//_____________
//remote
- (IBAction)changeRemoteInfo:(id)sender
{
    if (self.delegate)
    {
        [self.delegate changeRemoteInfo:self.remoteData cell:self];
    }
}

-(void)setRemoteCellData:(NSDictionary *)theData
{
    if (self.remoteData != theData )
    {
        self.remoteData = theData;
    }
    [self updateRemoteIcon];
}

- (void)updateRemoteIcon
{
    PersistableInfo *info = [DeviceListCell getRemoteInfo:self.remoteData];
    
    if ([info.icon isEqualToString:@"icon_ammeter"] ||[info.icon isEqualToString:@"icon_camera"]||[info.icon isEqualToString:@"sur_0"]||[info.icon isEqualToString:@"sur_1"]||[info.icon isEqualToString:@"sur_2"])
    {
        self.icon.image = [UIImage imageNamed:info.icon];
    }
    else
    {
        self.icon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@close",info.icon]];
    }
    
    //强制使用遥控
    self.icon.image = [UIImage imageNamed:@"icon_infrared"];

    self.labelName.text = [self.remoteData objectForKey:@"Alias"];
}

+(PersistableInfo *)getRemoteInfo:(NSDictionary *)Rdata
{
    NSString *uuid = [Rdata objectForKey:@"uuid"];
    NSUInteger index = [[Rdata objectForKey:@"indexPath"] intValue];
    PersistableInfo *info = (PersistableInfo *)[PersistableInfo findFirstByCriteria:@"where uuid='%@'",uuid];
    if (!info)
    {
        info = [[PersistableInfo alloc]init];
        NSLog(@"%@",[NSString stringWithFormat:@"sur_%d",index%3]);
        info.icon= [NSString stringWithFormat:@"sur_%d",index%3];
        info.uuid = uuid;
        info.cid = [Rdata objectForKey:@"ZID"];
        info.name = [Rdata objectForKey:@"Alias"];
        [info save];
    }
    
    return info;
}

//camera
- (IBAction)changeInfo:(id)sender
{
    if (self.delegate)
    {
        [self.delegate changeCameraInfo:self.cameraData cell:self];
    }
}

+(PersistableInfo *)getCameraInfo:(NSDictionary *)cdata
{
    NSString *uuid = [cdata objectForKey:@"uuid"];
    
    PersistableInfo *info = (PersistableInfo *)[PersistableInfo findFirstByCriteria:@"where uuid='%@'",uuid];
    if (!info)
    {
        info = [[PersistableInfo alloc]init];
        info.icon= @"icon_camera";
        info.uuid = uuid;
        info.zid = [cdata objectForKey:@"ZID"];
        info.name = [cdata objectForKey:@"Alias"];
        [info save];
    }
    
    return info;
}

-(void)setCameraCellData:(NSDictionary *)theData
{
    if (self.cameraData != theData )
    {
        self.cameraData = theData;
    }
    [self updateIcon];
}

- (void)updateIcon
{
    PersistableInfo *info = [DeviceListCell getCameraInfo:self.cameraData];
    if ([info.icon isEqualToString:@"icon_ammeter"] ||[info.icon isEqualToString:@"icon_camera"]||[info.icon isEqualToString:@"sur_0"]||[info.icon isEqualToString:@"sur_1"]||[info.icon isEqualToString:@"sur_2"])
    {
        self.icon.image = [UIImage imageNamed:info.icon];
    }
    else
    {
        self.icon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@close",info.icon]];
    }

//    self.icon.image = [UIImage imageNamed:info.icon];
    self.labelName.text = [self.cameraData objectForKey:@"Alias"];
}
//_____________

-(void)loadArmData
{
    CommonData *commonData = [[CommonData alloc] initWidthUrl:@"cgi-bin/Get.cgi?get=CD"];
    commonData.justCommand = YES;
    [commonData getDataWithDataRecivedBlock:^(id xdata) {
        NSString *result = [xdata objectForKey:@"result"];
        if ([result isEqualToString:@"cdoff"]) {
            btn_ds_arm.selected = YES;
        }else if ([result isEqualToString:@"cdon"]) {
            btn_arm.selected = YES;
        }
    }];
}


-(IBAction)setArm:(UIButton *)sender
{
    NSMutableString *url = [NSMutableString stringWithString:@"cgi-bin/Mode.cgi?MODE="];
    int value = sender.tag;
    if (sender.tag == 1) {
        [url appendString:@"CDON"];
    }else{
        [url appendString:@"CDOFF"];
    }
    
    CommonData *commonData = [[CommonData alloc] initWidthUrl:url];
    commonData.justCommand = YES;
    commonData.newValue = value;
    [commonData getDataWithDataRecivedBlock:^(id xdata) {
        NSString *result = [xdata objectForKey:@"result"];
        int newValue = [[xdata objectForKey:@"newValue"]intValue];
        if ([result isEqualToString:@"success"]) {
            if (newValue==0) {
                btn_ds_arm.selected = YES;
                btn_arm.selected = NO;
            }else{
                btn_ds_arm.selected = NO;
                btn_arm.selected = YES;
            }
        }
    }];
}

@end
